<?php

	require('../../database/bd_connect.php');

		if (isset($_GET['cpe_id'])){
					$cpe_id = $_GET['cpe_id'];
					$cpe_ssid = $_GET['cpe_ssid'];
					$cpe_mac = $_GET['cpe_mac'];
					$query = $_GET['query'];

	$cpe_sliced = strstr($cpe_ssid, '-', true);
	$first = 0 ;
	$last_time = 0;
	$enter = 0 ;

	if($query==1){	//$sql = "SELECT DISTINCT ssid, mac, DATE_FORMAT(time, '%H:%i') as time FROM wifi_data WHERE ssid NOT LIKE 'FON_ZON_FREE_INTERNET' AND ssid NOT LIKE 'MEO-WIFI%' AND cpe_id=".$cpe_id." AND time >= NOW() - INTERVAL 10 MINUTE group by mac ORDER BY ssid ASC";
		$sql = "SELECT DISTINCT ssid,mac, DATE_FORMAT(time, '%H:%i') as time FROM wifi_data WHERE ssid NOT LIKE 'FON_ZON_FREE_INTERNET' AND ssid NOT LIKE 'MEO-WIFI%' AND cpe_id=".$cpe_id." AND time BETWEEN DATE_SUB(NOW(), INTERVAL 1 MINUTE) AND DATE_ADD(NOW(), INTERVAL 0 MINUTE) group by mac, minute(time) order by minute(time),ssid ASC";
	}
	if($query==2){	//$sql = "SELECT DISTINCT ssid, mac, DATE_FORMAT(time, '%M %d') as time FROM wifi_hour WHERE ssid NOT LIKE 'FON_ZON_FREE_INTERNET' AND ssid NOT LIKE 'MEO-WIFI%' AND cpe_id=".$cpe_id." AND time >= NOW() - INTERVAL 6 DAY group by mac order by ssid ASC";
		$sql = "SELECT DISTINCT ssid,mac, DATE_FORMAT(time, '%M %d') as time FROM wifi_hour WHERE ssid NOT LIKE 'FON_ZON_FREE_INTERNET' AND ssid NOT LIKE 'MEO-WIFI%' AND cpe_id=".$cpe_id." AND time >= DATE_SUB(CURDATE(), INTERVAL 0 DAY) group by mac, day(time) order by date(time),ssid ASC";
	}
	if($query==3){	//$sql = "SELECT DISTINCT ssid, mac, DATE_FORMAT(time, 'Week %U in %M') as time  FROM wifi_hour WHERE ssid NOT LIKE 'FON_ZON_FREE_INTERNET' AND ssid NOT LIKE 'MEO-WIFI%' AND cpe_id=".$cpe_id." AND time >= NOW() - INTERVAL 3 WEEK group by mac order by ssid ASC";
		$sql = "SELECT DISTINCT ssid,mac, DATE_FORMAT(time, 'Week %U in %M') as time FROM wifi_hour WHERE ssid NOT LIKE 'FON_ZON_FREE_INTERNET' AND ssid NOT LIKE 'MEO-WIFI%' AND cpe_id=".$cpe_id." AND YEARWEEK(`time`, 1) = YEARWEEK(CURDATE(), 1) group by mac, WEEK(time) order by WEEK(time),ssid ASC";
	}

	$result = $conn->query($sql);

				echo 	'<div class="panel panel-default">
						<table class="table grid table-striped table-hover">
						<tr>
					    <th>SSID</th>
					    <th>MAC</th>
					    <th>Service Provider</th>
					  	</tr>';	
	while ($row = mysqli_fetch_array($result)) {
		if ((strpos($row['ssid'],$cpe_sliced) === 0) && $enter == 0) {
			echo "<tr>";
			echo "<td><b>".$cpe_ssid . "<b></td>" ;
			echo "<td>".$cpe_mac . "</td>" ;
				if (strpos($row['ssid'], "Vodafone") === 0) {
					echo "<td>Vodafone</td>"  ;
				}
				elseif (strpos($row['ssid'], "MEO") === 0) {
					echo "<td>PT</td>"  ;
				}
				elseif ((strpos($row['ssid'], "NOS") === 0) || (stripos($row['ssid'], "ZON") === 0)){
					echo "<td>NOS</td>"  ;
				}
				else  {
					echo "<td>Others</td>"  ;
				}
				//echo "<td>".$row['time'] . "</td>" ;
			echo "</tr>";
			$enter = 1 ;
		}
		echo "<tr>";
		echo "<td><b>".$row['ssid'] . "</b></td>" ;
		echo "<td>".$row['mac'] . "</td>" ;
						if (stripos($row['ssid'], "Vodafone") === 0) {
					echo "<td>Vodafone</td>"  ;
				}
				elseif (stripos($row['ssid'], "MEO") === 0) {
					echo "<td>PT</td>"  ;
				}
				elseif ((stripos($row['ssid'], "NOS") === 0) || (stripos($row['ssid'], "ZON") === 0)){
					echo "<td>NOS</td>"  ;
				}
				else  {
					echo "<td>Others</td>"  ;
				}	
				//echo "<td>".$row['time'] . "</td>" ;
			echo "</tr>";
		echo "</tr>";
		/*
		if($row['time']!=$last_time && $first == 0){
			$last_time=$row['time'];
			$first = 1 ;
		}
		else
			break ;*/
	}
			if ($enter == 0) {
			echo "<tr>";
			echo "<td><b>".$cpe_ssid . "</b></td>" ;
			echo "<td>".$cpe_mac . "</td>" ;
									if (strpos($cpe_ssid, "Vodafone") === 0) {
					echo "<td>Vodafone</td>"  ;
				}
				elseif (stripos($cpe_ssid, "MEO") === 0) {
					echo "<td>PT</td>"  ;
				}
				elseif ((stripos($cpe_ssid, "NOS") === 0) || (strpos($cpe_ssid, "ZON") === 0)){
					echo "<td>NOS</td>"  ;
				}
				else  {
					echo "<td>Others</td>"  ;
				}	
				//echo "<td>".$row['time'] . "</td>" ;
			echo "</tr>";
			$enter = 1 ;
		}
					echo 	'</table>
						</div>';
	mysqli_close($conn);
	}
	else
		echo "erro";

?>